package cz.spock.cms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.googlecode.objectify.Key;

import cz.spock.cms.entity.content.Field;
import cz.spock.cms.service.FieldService;
import cz.spock.cms.service.NodeService;
import cz.spock.core.controller.BaseController;

/**
 * serves field related requests
 * 
 * TODO add support for field lists...
 * 
 * TODO handle errors
 * 
 * @author miso
 *
 */
@Controller 
public class FieldController extends BaseController {

    @Autowired
    private FieldService fService;
    @Autowired
    private NodeService nService;
    
    @RequestMapping(value = "/cms/{node}/fields", method = RequestMethod.GET)
    public ModelAndView nodeFields(@PathVariable String node, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + node + "/fields");
        // find all nodes for current project
        // TODO find project by id, then by name... and result use for node search
        List<Field> fl = new ArrayList<Field>();
        List<Key<Field>> lfk = nService.getNodeById(Long.parseLong(node)).getContentFields();
        if(lfk == null) {
            lfk = new ArrayList<Key<Field>>();
        } else {
            for(Key<Field> fk : lfk) {
                fl.add(fService.getFieldById(fk.getId()));
            }
        }
        return new ModelAndView("cms/field/fields", "fields", fl).addObject("nodeId", node);
    }
    
    @RequestMapping(value = "/cms/field", method = RequestMethod.GET)
    public String field(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/field");
        // find project by id
        
        return "cms/field/field";
    }
    
    @RequestMapping(value = "/cms/{node}/field/create", method = RequestMethod.GET)
    public ModelAndView fieldCreateGet(@PathVariable String node, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + node + "/field/create get");
        return new ModelAndView("cms/field/create", "nodeId", node);
    }
    
    @RequestMapping(value = "/cms/{node}/field/create", method = RequestMethod.POST)
    public String fieldCreatePost(@PathVariable Long node, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + node + "/field/create post");
        // create field
        try {
            fService.createField(request.getParameter("label"), 
                    request.getParameter("value"), node);
        } catch(Exception e) {            
            sendJson(response, fService.errorToJson(e.getMessage(), 0));
            return null;
        }
        return "redirect:/cms/" + node + "/fields";
    }
    
}
